#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

// 290.单词规律
// 给定一种规律 pattern 和一个字符串 s ，判断 s 是否遵循相同的规律。

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        vector<string> words = split(s);
        if(pattern.size() != words.size())
            return false;

        unordered_map<char, string> map1;
        unordered_map<string, char> map2;
        for(int i = 0; i < pattern.size(); i++)
            if(map1.find(pattern[i]) == map1.end()) {
                if(map2.find(words[i]) != map2.end())
                    return false;
                map1[pattern[i]] = words[i];
                map2[words[i]] = pattern[i];
            } else {
                string s = map1[pattern[i]];
                if(s != words[i])
                    return false;
            }

        return true;
    }

private:
    vector<string> split(const string& s) {
        vector<string> res;
        int start = 0;
        for(int i = 0; i < s.size(); i++)
            if(s[i] != ' ') {
                start = i;
                break;
            }
        if(start == s.size())
            return res;
        for(int i = start + 1; i <= s.size(); )
            if(i == s.size() || s[i] == ' ') {
                res.push_back(s.substr(start, i - start));
                start = i + 1;
                i = start + 1;
            } else
                i++;
        return res;
    }
};

void print_bool(bool res){
    cout << (res ? "True" : "False") << endl;
}

int main() {
    string pattern1 = "abba";
    string str1 = "dog cat cat dog";
    print_bool(Solution().wordPattern(pattern1, str1));

    string pattern2 = "abba";
    string str2 = "dog cat cat fish";
    print_bool(Solution().wordPattern(pattern2, str2));

    return 0;
}
